﻿using PropertyDataContext.Model;
using PropertyDataContext.UnitOfWork;
using PropertyEntities.RequestEntities;
using PropertyEntities.ResponseEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PropertyDAL
{
    class LoginDAL
    {
        UOWEntities uow = new UOWEntities();
        public LoginResponse Login(LoginRequest loginRequest)
        {
            LoginResponse loginResponse = new LoginResponse();

            try
            {

                if (loginRequest.userType == UserType.Admin)
                {
                    Admin admin = uow.adminRepositery.Get().Where(ad => ad.Username == loginRequest.Username && ad.Password == loginRequest.Password).FirstOrDefault();
                    if (admin != null)
                    {
                        loginResponse.Username = admin.Username;
                        loginResponse.SuccessMessage = "Username logged in.";
                        loginResponse.IsResult = true;
                    }
                    else
                    {
                        loginResponse.ErrorMessage = "Record doesn't exists.";
                        loginResponse.IsResult = false;
                    }
                }
                else
                    if (loginRequest.userType == UserType.Agent)
                    {
                        Agent agent = uow.agentRepositery.Get().Where(ad => ad.Username == loginRequest.Username && ad.Password == loginRequest.Password).FirstOrDefault();
                        if (agent != null)
                        {
                            loginResponse.Username = agent.Username;
                            loginResponse.SuccessMessage = "Username logged in.";
                            loginResponse.IsResult = true;
                        }
                        else
                        {
                            loginResponse.ErrorMessage = "Record doesn't exists.";
                            loginResponse.IsResult = false;
                        }
                    }

            }
            catch
            {
                loginResponse.ErrorMessage = "Some error occured.";
                loginResponse.IsResult = false;
            }

            return loginResponse;
        }
    }
}
